package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.FollowsArticleData;

/**
 * 关注动态下文章列表的适配器
 * Created by lanouhn on 16/8/31.
 */
public class MyFollowsArticlesAdapter extends RecyclerView.Adapter<MyFollowsArticlesAdapter.ViewHolder> {
    private Context context;
    private FollowsArticleData followsArticleData;
    private OnRVItemClickListener clickListener;
    private OnRVItemLongClickListener longClickListener;

    public void setClickListener(OnRVItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setLongClickListener(OnRVItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public MyFollowsArticlesAdapter(Context context, FollowsArticleData followsArticleData) {
        this.context = context;
        this.followsArticleData = followsArticleData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_follows_article, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (followsArticleData != null) {
            Picasso.with(context).load(followsArticleData.getData().getTopic().get(position).getUser().getAvatar()).config(Bitmap.Config.RGB_565).into(holder.iv_header);
            if ("".equals(followsArticleData.getData().getTopic().get(position).getPic())) {
                Picasso.with(context).load(followsArticleData.getData().getTopic().get(position).getPics().get(0).getUrl()).config(Bitmap.Config.RGB_565).into(holder.iv_bigpic);
            } else {
                Picasso.with(context).load(followsArticleData.getData().getTopic().get(position).getPic()).config(Bitmap.Config.RGB_565).into(holder.iv_bigpic);
            }
            holder.tv_username.setText(followsArticleData.getData().getTopic().get(position).getUser().getNickname());
            holder.tv_timebefore.setText(followsArticleData.getData().getTopic().get(position).getOrder_time_str());
            holder.tv_title.setText(followsArticleData.getData().getTopic().get(position).getTitle());
            holder.tv_content.setText(followsArticleData.getData().getTopic().get(position).getDesc());
            holder.tv_views.setText(followsArticleData.getData().getTopic().get(position).getViews());
            if ("".equals(followsArticleData.getData().getTopic().get(position).getComments())) {
                holder.tv_comments.setText("0");
            } else {
                holder.tv_comments.setText(followsArticleData.getData().getTopic().get(position).getComments());
            }
        }
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition();
                    clickListener.OnRVItemClickListener(clickposition);
                }
            });
        }
        if (longClickListener != null) {
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        int clickposition = holder.getLayoutPosition();
                        longClickListener.OnRVItemLongClickListener(clickposition);
                    }
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return followsArticleData.getData().getTopic().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_header, iv_bigpic;
        private TextView tv_username, tv_timebefore, tv_title, tv_content, tv_views, tv_comments;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_header = (ImageView) itemView.findViewById(R.id.iv_header);
            iv_bigpic = (ImageView) itemView.findViewById(R.id.iv_bigpic);
            tv_username = (TextView) itemView.findViewById(R.id.tv_username);
            tv_timebefore = (TextView) itemView.findViewById(R.id.tv_timebefore);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_views = (TextView) itemView.findViewById(R.id.tv_views);
            tv_comments = (TextView) itemView.findViewById(R.id.tv_comments);
        }
    }
}
